package com.hxm.appGenealogy.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.hxm.appGenealogy.Function.BrothersSistersPeopleFunction;
import com.hxm.appGenealogy.Function.FamilyFunction;
import com.hxm.appGenealogy.Function.PeopleFunction;
import com.hxm.appGenealogy.Function.PeopleFunctionService;
import com.hxm.appGenealogy.config.Constant;
import com.hxm.appGenealogy.config.ParmIntended;
import com.hxm.appGenealogy.config.queryVo.QueryWrapperVO;
import com.hxm.appGenealogy.config.tree.TreeConfig;
import com.hxm.appGenealogy.entity.*;
import com.hxm.appGenealogy.mapper.PeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;

@Service
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW)
public class TreeService {

    @Autowired
    private PeopleFunction peopleFunction;


    @Autowired
    private PeopleFunctionService peopleFunctionService;

    @Autowired
    private BrothersSistersPeopleFunction brothersSistersPeopleFunction;

    @Autowired
    private TreeConfig treeConfig;

    @Autowired
    private FamilyFunction familyFunction;

    @Autowired
    private PeopleMapper peopleMapper;

    /**
     * 添加人的信息;
     *
     * @param people
     * @return
     */
    public ResponDataVo addPeople(People people) {

        People people1 = peopleFunction.insertPeople(people);
        return new ResponDataVo(people1);
    }

    /**
     * 添加父亲的信息,判断当前登录的账号是否有权限,在次判断当前的添加父亲的孩子是否有母亲,如果有母亲的话
     * 在添加母亲的时候就已经添加过母亲孩子的数据,在当前数据的基础,修改这条数据添加上这个孩子父亲的信息,
     * 在次判断我是否有兄弟姐妹,如果有兄弟姐妹的话,将我的兄弟姐妹添加到父亲下,判断我父亲是否有媳妇,如果有的话
     * 说明孩子在添加母亲的时候已经添加过我兄弟姐妹的数据,将我母亲下孩子的数据查询出来进行修改,添加我父亲的数据;
     *
     * @param peopleVo
     * @return
     */
    public ResponDataVo addFather(PeopleVo peopleVo) {
        //参数信息的教验证;
        String parmAnalysis = ParmIntended.parmAnalysis(peopleVo, Constant.myPeopleId, "tenementId", "name", "gender");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断当前的登录者是否有添加的权限;
        People people = peopleFunction.peopleGetByid(peopleVo.getTenementId());
        Long familyId = people.getFamilyId();
        if (familyId == null) {
            return new ResponDataVo("请创建家族后在进行添加");
        }
        Family family = (Family) familyFunction.selectByid(people.getFamilyId()).getData();
        if (family == null) {
            return new ResponDataVo("没有当前的家族;");
        }
        if (people.getJurisdiction() == 2) {
            return new ResponDataVo("您没有权限进行添加;");
        }
        //获取到我的信息;
        People peopleSon = peopleFunction.peopleGetByid(peopleVo.getMyPeopleId());
        if (peopleSon == null) {
            return new ResponDataVo(Constant.errorMessagePeople);
        }
        //判断我是否有父亲;
        if (peopleSon.getFatherPeopleId() != null) {
            return new ResponDataVo("只能有一个父亲;");
        }
        //设备父亲的性别为男;
        peopleVo.setGender(Constant.genderboy);
        //设置他的家族;
        peopleVo.setFamilyId(people.getFamilyId());
        //设置父亲的权限为无权限;
        peopleVo.setJurisdiction(2);
        //判断我是否有母亲;
        Boolean flag = false;
        if (peopleSon.getMotherPeopleId() != null) {
            peopleVo.setWifePeopleId(peopleSon.getMotherPeopleId());
            flag = true;
        }

        People peopleFather = ParmIntended.removePeopleRequestId(peopleVo);
        //添加父亲的数据;
        People insertPeopleFather = peopleFunction.insertPeople(peopleFather);
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        //设置顶级的父类的数据;
        treeConfig.addTop(insertPeopleFather, family);
        //判断我是否有兄弟姐妹,将我的兄弟姐妹添加到我的父亲下面;
        List<BrothersSistersPeople> brothersSistersPeople = brothersSistersPeopleFunction.slectStringQuery(JSON.toJSONString(
                new QueryWrapperVO().eq("my_people_id", peopleSon.getId())));
        if (brothersSistersPeople.size() > 0) {
            //将我的兄弟姐妹添加到父亲下面还有母亲下面;
            treeConfig.addSonOrGirl(insertPeopleFather, brothersSistersPeople);
        }
        //说明我在添加母亲的时间添加过孩子的数据;
        if (flag == true) {
            //我如果存在父亲将母亲设置为父亲的妻子;
            People peopleGetByidFather = peopleFunction.peopleGetByid(peopleSon.getMotherPeopleId());
            peopleGetByidFather.setHusbandPeopleId(peopleFather.getId());
            peopleFunction.updateByid(peopleGetByidFather);
            //将我的兄弟们添加到母亲的下面;
            PeopleService peopleService = peopleFunctionService.getQueryOne(JSON.toJSONString(new QueryWrapperVO()
                    .eq("people_monther_id", insertPeopleFather.getWifePeopleId())
                    .eq("people_below_id", peopleSon.getId())
            ));
            peopleService.setPeopleFatherId(insertPeopleFather.getId());
            peopleFunctionService.updateByid(peopleService);
        } else {
            //如果我没有母亲的话;
            PeopleService peopleService = new PeopleService();
            peopleService.setPeopleBelowId(peopleSon.getId());
            peopleService.setPeopleFatherId(insertPeopleFather.getId());
            if (peopleSon.getGender() == Constant.genderboy) {
                peopleService.setType(Constant.son);
            } else {
                peopleService.setType(Constant.daughter);
            }
            peopleFunctionService.insertPeople(peopleService);
        }
        //如果添加的女人的话;

        //修改我的信息添加上我的阜新;
        peopleSon.setFatherPeopleId(insertPeopleFather.getId());
        peopleFunction.updateByid(peopleSon);


        return new ResponDataVo(true);
    }

    /**
     * 添加父亲修改2;
     *
     * @param peopleVo
     * @return
     */
    public ResponDataVo addFather2(PeopleVo peopleVo) {
        //参数信息的教验证;
        String parmAnalysis = ParmIntended.parmAnalysis(peopleVo, Constant.myPeopleId, "tenementId", "name", "gender");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断当前的登录者是否有添加的权限;
        People people = peopleFunction.peopleGetByid(peopleVo.getTenementId());
        Long familyId = people.getFamilyId();
        if (familyId == null) {
            return new ResponDataVo("请创建家族后在进行添加");
        }
        Family family = (Family) familyFunction.selectByid(people.getFamilyId()).getData();
        if (family == null) {
            return new ResponDataVo("没有当前的家族;");
        }
        if (people.getJurisdiction() == 2) {
            return new ResponDataVo("您没有权限进行添加;");
        }
        //获取到我的信息;
        People peopleSonOrGirl = peopleFunction.peopleGetByid(peopleVo.getMyPeopleId());
        if (peopleSonOrGirl == null) {
            return new ResponDataVo(Constant.errorMessagePeople);
        }
        //判断我是否有父亲;
        if (peopleSonOrGirl.getFatherPeopleId() != null) {
            return new ResponDataVo("您只能添加一个父亲;");
        }
        peopleVo.setJurisdiction(2);
        peopleVo.setFamilyId(family.getId());
        People peopleRequestId = ParmIntended.removePeopleRequestId(peopleVo);
        People peopleFather = peopleFunction.insertPeople(peopleRequestId);
        //判断是男还是女;
        Boolean wife = treeConfig.isWife(peopleSonOrGirl);
        People peopleHush = null;
        if (wife == true) {
            //查询出来我的丈夫;
            Long husbandPeopleId = peopleSonOrGirl.getHusbandPeopleId();
            peopleHush = peopleFunction.peopleGetByid(husbandPeopleId);
            peopleHush.setFatherPeopleId(peopleFather.getId());
            peopleFunction.updateByid(peopleHush);
            //他自己也要设置父亲还有母亲的信息;
            peopleSonOrGirl.setFatherPeopleId(peopleFather.getId());
            peopleFunction.updateByid(peopleSonOrGirl);
        } else {
            //获取到当前添加人的性别;
            if (peopleSonOrGirl.getGender().equals(Constant.genderGirl)) {
                //判断他是否有丈夫;
                if (peopleSonOrGirl.getHusbandPeopleId() != null) {
                    People peopleGetByid = peopleFunction.peopleGetByid(peopleSonOrGirl.getHusbandPeopleId());
                    peopleGetByid.setFatherPeopleId(peopleFather.getId());
                    peopleFunction.updateByid(peopleGetByid);
                }
            } else {
                //如果是男的;
                if (peopleSonOrGirl.getWifePeopleId() != null) {
                    People peopleGetByid = peopleFunction.peopleGetByid(peopleSonOrGirl.getWifePeopleId());
                    peopleGetByid.setFatherPeopleId(peopleFather.getId());
                    peopleFunction.updateByid(peopleGetByid);
                }
            }
            //将我和我的父亲之间做一个相互的绑定;
            peopleSonOrGirl.setFatherPeopleId(peopleFather.getId());
            peopleFunction.updateByid(peopleSonOrGirl);
        }
        QueryWrapperVO queryWrapperVO = new QueryWrapperVO();
        //将我和我的兄弟们设置为父亲的儿子;
        if (wife == true) {
            //将我我的丈夫设置为我父亲的儿子;
            treeConfig.addSonOrGirlOne(peopleHush, peopleFather);
            queryWrapperVO.eq("my_people_id", peopleHush.getId());
        } else {
            //将我也添加到父亲的下面去;
            treeConfig.addSonOrGirlOne(peopleSonOrGirl, peopleFather);
            queryWrapperVO.eq("my_people_id", peopleSonOrGirl.getId());
        }
        List<BrothersSistersPeople> brothersSistersPeople = brothersSistersPeopleFunction.slectStringQuery(JSON.toJSONString(queryWrapperVO));
        if (brothersSistersPeople.size() > 0) {
            treeConfig.addSonOrGirl(peopleFather, brothersSistersPeople);
        }
        //设置父亲为顶级的人;
        family.setTopPeopleId(peopleFather.getId());
        familyFunction.updateById(family);
        return new ResponDataVo(peopleFather);
    }

    /**
     * 添加母亲;
     * 同添加父亲一个道理;
     */
    public ResponDataVo addMonther(PeopleVo peopleVo) {
        //参数信息的教验证;
        String parmAnalysis = ParmIntended.parmAnalysis(peopleVo, Constant.myPeopleId, "tenementId", "name", "gender");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断当前的登录者是否有添加的权限;
        People people = peopleFunction.peopleGetByid(peopleVo.getTenementId());
        Long familyId = people.getFamilyId();
        if (familyId == null) {
            return new ResponDataVo("请创建家族后在进行添加");
        }
        Family family = (Family) familyFunction.selectByid(people.getFamilyId()).getData();
        if (family == null) {
            return new ResponDataVo("没有当前的家族;");
        }
        if (people.getJurisdiction() == 2) {
            return new ResponDataVo("您没有权限进行添加;");
        }
        //获取到我的信息;
        People peopleSon = peopleFunction.peopleGetByid(peopleVo.getMyPeopleId());
        if (peopleSon == null) {
            return new ResponDataVo(Constant.errorMessagePeople);
        }
        if (peopleSon.getMotherPeopleId() != null) {
            return new ResponDataVo("您只能添加一个母亲");
        }
        //设置他的家族;
        peopleVo.setFamilyId(people.getFamilyId());
        //设置父亲的权限为无权限;
        peopleVo.setJurisdiction(2);
        People peopleMonther = ParmIntended.removePeopleRequestId(peopleVo);
        Boolean flag = false;
        //判断我是否有父亲;
        if (peopleSon.getFatherPeopleId() != null) {
            //添加母亲的丈夫;
            peopleMonther.setFatherPeopleId(peopleSon.getFatherPeopleId());
            flag = true;
        }
        //添加母亲的数据;
        People insertPeopleMonther = peopleFunction.insertPeople(peopleMonther);
        People peopleGetHus = null;
        //父亲绑定母亲母亲绑定父亲,并且设置家族的权限问题panduan
        if (peopleSon.getHusbandPeopleId() != null) {
            peopleGetHus = peopleFunction.peopleGetByid(peopleSon.getHusbandPeopleId());
            peopleGetHus.setMotherPeopleId(insertPeopleMonther.getId());
            peopleFunction.updateByid(peopleGetHus);
            //设置家族的最高的权限;
            People peopleTop = null;
            if (flag == true) {
                peopleTop = peopleFunction.peopleGetByid(peopleSon.getFatherPeopleId());
                treeConfig.addTop(peopleTop, family);
            } else {
                treeConfig.addTop(insertPeopleMonther, family);
            }

        } else {
            peopleSon.setMotherPeopleId(insertPeopleMonther.getId());
            peopleFunction.updateByid(peopleSon);
            //设置家族的最高的权限;
            treeConfig.addTop(insertPeopleMonther, family);
        }

        //判断我是否有兄弟姐妹,将我的兄弟姐妹添加到我的父亲还有母亲下面;
        List<BrothersSistersPeople> brothersSistersPeople = brothersSistersPeopleFunction.slectStringQuery(JSON.toJSONString(new QueryWrapperVO().eq("my_people_id", peopleSon.getId())));
        if (brothersSistersPeople.size() > 0) {
            //将我的兄弟姐妹添加到父亲下面还有母亲/父亲下面;
            treeConfig.addSonOrGirsMonther(insertPeopleMonther, brothersSistersPeople);
        }
        //如果有丈夫的话,判断我丈夫之前是否添加过这个数据的信息;
        if (flag == true) {
            //判断我的父亲之前是否添加过我的信息;
            PeopleService peopleService = peopleFunctionService.getQueryOne(JSON.toJSONString(new QueryWrapperVO()
                    .eq("people_father_id", insertPeopleMonther.getHusbandPeopleId())
                    .eq("people_below_id", peopleSon.getId())
            ));
            peopleService.setPeopleMontherId(insertPeopleMonther.getId());
            peopleFunctionService.updateByid(peopleService);
        } else {
            PeopleService peopleService = new PeopleService();
            //设置母亲的信息;
            peopleService.setPeopleMontherId(insertPeopleMonther.getId());
            //设置性别;
            if (peopleSon.getGender().equals(Constant.genderboy)) {
                peopleService.setType(Constant.son);
            } else {
                peopleService.setType(Constant.daughter);
            }
            //判断添加的人是儿子还是女儿;
            if (peopleSon.getGender().equals(Constant.genderGirl)) {
                //判断我是否有丈夫;
                if (peopleSon.getHusbandPeopleId() != null) {
                    People peopleHusband = peopleFunction.peopleGetByid(peopleSon.getHusbandPeopleId());
                    peopleService.setPeopleBelowId(peopleHusband.getId());
                }
            } else {
                peopleService.setPeopleBelowId(peopleSon.getId());
            }
            peopleFunctionService.insertPeople(peopleService);
        }

        return new ResponDataVo(peopleMonther);
    }


    /**
     * 在次修改添加母亲;
     *
     * @param peopleVo
     * @return
     */
    public ResponDataVo addMonther2(PeopleVo peopleVo) {
        //参数信息的教验证;
        String parmAnalysis = ParmIntended.parmAnalysis(peopleVo, Constant.myPeopleId, "tenementId", "name", "gender");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断当前的登录者是否有添加的权限;
        People people = peopleFunction.peopleGetByid(peopleVo.getTenementId());
        Long familyId = people.getFamilyId();
        if (familyId == null) {
            return new ResponDataVo("请创建家族后在进行添加");
        }
        Family family = (Family) familyFunction.selectByid(people.getFamilyId()).getData();
        if (family == null) {
            return new ResponDataVo("没有当前的家族;");
        }
        if (people.getJurisdiction() == 2) {
            return new ResponDataVo("您没有权限进行添加;");
        }
        //设置母亲的权限;
        peopleVo.setJurisdiction(2);
        //获取到我的信息;
        People peopleSonOGirl = peopleFunction.peopleGetByid(peopleVo.getMyPeopleId());
        //设置家族的信息;
        peopleVo.setFamilyId(family.getId());
        if (peopleSonOGirl == null) {
            return new ResponDataVo(Constant.errorMessagePeople);
        }
        if (peopleSonOGirl.getMotherPeopleId() != null) {
            return new ResponDataVo("您只能有一个母亲;");
        }
        //判断我是不是有父亲;
        if (peopleSonOGirl.getFatherPeopleId() == null) {
            return new ResponDataVo("请先添加父亲");
        } else {
            //设置母亲的丈夫;
            peopleVo.setHusbandPeopleId(peopleSonOGirl.getFatherPeopleId());
            //判断丈夫是否有父亲或者是母亲;
            People peopleHus = peopleFunction.peopleGetByid(peopleSonOGirl.getFatherPeopleId());
            if (peopleHus.getFatherPeopleId() != null) {
                peopleVo.setFatherPeopleId(peopleHus.getFatherPeopleId());
            } else if (peopleHus.getMotherPeopleId() != null) {
                peopleVo.setMotherPeopleId(peopleHus.getMotherPeopleId());
            }
        }
        //添加母亲的信息;
        People peopleMontherData = ParmIntended.removePeopleRequestId(peopleVo);
        People peopleMonther = peopleFunction.insertPeople(peopleMontherData);

        //设置我父亲的妻子是我得到母亲;
        People peopleFather = peopleFunction.peopleGetByid(peopleSonOGirl.getFatherPeopleId());
        peopleFather.setWifePeopleId(peopleMonther.getId());
        peopleFunction.updateByid(peopleFather);
        //设置我为我的上一级是母亲;
        peopleSonOGirl.setMotherPeopleId(peopleMonther.getId());
        peopleFunction.updateByid(peopleSonOGirl);

        if (peopleSonOGirl.getHusbandPeopleId() != null) {
            Long husbandPeopleId = peopleSonOGirl.getHusbandPeopleId();
            People peopleHysban = peopleFunction.peopleGetByid(husbandPeopleId);
            peopleHysban.setMotherPeopleId(peopleMonther.getId());
            peopleFunction.updateByid(peopleHysban);
        }
        //查询出来父亲添加的儿子的信息,设置上母亲的信息;
        List<PeopleService> people_father_id = peopleFunctionService.slectStringLamQuery(JSON.toJSONString(new QueryWrapperVO()
                .eq("people_father_id", peopleFather.getId())

        ));
        for (PeopleService peopleService : people_father_id) {
            peopleService.setPeopleMontherId(peopleMonther.getId());
            peopleFunctionService.updateByid(peopleService);
        }
        return new ResponDataVo(peopleMonther);

    }

    /**
     * 添加我的兄弟姐妹,判断我是否有父亲有的话,添加到我的父亲里面去,如果有母亲的话,添加他的信息指定母亲,
     * 在将新添加的兄弟添加到我的下面去;
     *
     * @param peopleVo
     * @return
     */
    public ResponDataVo addBrother(PeopleVo peopleVo) {
        //判断是否缺少参数;
        String parmAnalysis = ParmIntended.parmAnalysis(peopleVo, Constant.myPeopleId, Constant.NAME, "tenementId", "gender");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断当前的登录者是否有添加的权限;
        People people = peopleFunction.peopleGetByid(peopleVo.getTenementId());
        Long familyId = people.getFamilyId();
        if (familyId == null) {
            return new ResponDataVo("请创建家族后在进行添加");
        }
        if (people.getJurisdiction() == 2) {
            return new ResponDataVo("您没有权限进行添加;");
        }
        Family family = (Family) familyFunction.selectByid(familyId).getData();
        //获取到我的信息;
        People myPeople = peopleFunction.peopleGetByid(peopleVo.getMyPeopleId());
        if (myPeople == null) {
            return new ResponDataVo(Constant.errorMessagePeople);
        }

        if (myPeople.getId().equals(family.getTopPeopleId())) {
            return new ResponDataVo("您是最顶层的人,请先添加父亲在添加您的兄弟;");
        }
        //判断我的母亲是不是妻子,如果是妻子的话,根据他丈夫的信息查询兄弟添加到儿子下面;
        Boolean wife = treeConfig.isWife(myPeople);
        if (wife) {
            //获取他的丈夫;
            Long husbandPeopleId = myPeople.getHusbandPeopleId();
            if (husbandPeopleId.equals(family.getTopPeopleId())) {
                return new ResponDataVo("您是最顶层的人,请先添加父亲在添加您的兄弟;");
            }
        }
        //获取兄弟的信息;
        People peopleBrother = ParmIntended.removePeopleRequestId(peopleVo);
        Boolean blagMonther = false;
        //判断我是否有添加过母亲的数据;
        if (myPeople.getMotherPeopleId() != null) {
            peopleBrother.setMotherPeopleId(myPeople.getMotherPeopleId());
            blagMonther = true;
        }
        Boolean flagFather = false;
        //判断我是否添加过父亲的数据;
        if (myPeople.getFatherPeopleId() != null) {
            peopleBrother.setFatherPeopleId(myPeople.getFatherPeopleId());
            flagFather = true;
        }
        //设置兄弟的权限;
        peopleBrother.setJurisdiction(2);
        //设置兄弟所在的家族;
        peopleBrother.setFamilyId(family.getId());
        //添加兄弟的人员的信息;
        People insertPeopleBrother = peopleFunction.insertPeople(peopleBrother);
        //查询我的所有的兄弟;
        List<BrothersSistersPeople> brothersSistersPeopleList = brothersSistersPeopleFunction
                .slectStringQuery(JSON.toJSONString(new QueryWrapperVO()
                        .eq("my_people_id", myPeople.getId())
                ));
        //判断我是否有兄弟;
        if (brothersSistersPeopleList.size() > 0) {
            //将我新添加的兄弟和我的兄弟之间做一个相互的关系;
            treeConfig.addBrotherSistersAdd(insertPeopleBrother, brothersSistersPeopleList);
        }
        //将我和我的兄弟之间做一个相互的关系;
        treeConfig.addBrotherPeopleAndMyPeople(myPeople, insertPeopleBrother);
        //将我的兄弟添加到我的父母下面;-
        BrothersSistersPeople brothersSistersPeople = brothersSistersPeopleFunction.slectQueryGetOne(JSON.toJSONString(new QueryWrapperVO()
                .eq("my_people_id", myPeople.getId())
                .eq("brother_people_id", insertPeopleBrother.getId())
        ));
        List<BrothersSistersPeople> brothersSistersPeopleInsert = new ArrayList<>();
        brothersSistersPeopleInsert.add(brothersSistersPeople);

        if (flagFather == true) {
            Long fatherPeopleId = myPeople.getFatherPeopleId();
            People peopleFather = peopleFunction.peopleGetByid(fatherPeopleId);
            treeConfig.addSonOrGirl(peopleFather, brothersSistersPeopleInsert);
        }
        if (blagMonther == true) {
            Long montherPeopleId = myPeople.getMotherPeopleId();
            People peopleMonther = peopleFunction.peopleGetByid(montherPeopleId);
            treeConfig.addSonOrGirsMonther(peopleMonther, brothersSistersPeopleInsert);
        }
        return new ResponDataVo(insertPeopleBrother);
    }

    /**
     * 添加妻子,添加妻子的时候要注意,判断我是否有儿子,将儿子的数据添加到我妻妻子的下面去;
     * 如果儿子存在就进行添加如果不存在就不进行添加;
     *
     * @param peopleVo
     * @return
     */
    public ResponDataVo addWifePeopleId(@RequestBody PeopleVo peopleVo) {
        //判断是否缺少参数;
        String parmAnalysis = ParmIntended.parmAnalysis(peopleVo, Constant.myPeopleId, Constant.NAME, "tenementId", "gender");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断当前的登录者是否有添加的权限;
        People people = peopleFunction.peopleGetByid(peopleVo.getTenementId());
        Long familyId = people.getFamilyId();
        if (familyId == null) {
            return new ResponDataVo("请创建家族后在进行添加");
        }
        if (people.getJurisdiction() == 2) {
            return new ResponDataVo("您没有权限进行添加;");
        }
        //获取到我的信息;
        People myPeople = peopleFunction.peopleGetByid(peopleVo.getMyPeopleId());
        if (myPeople == null) {
            return new ResponDataVo(Constant.errorMessagePeople);
        }
        //判断我是否已经添加过了妻子;
        if (myPeople.getWifePeopleId() != null) {
            return new ResponDataVo("您已经有妻子了,不可以取两个;");
        }
        //设置妻子的权限;
        peopleVo.setJurisdiction(2);
        //设置妻子所在的家族;
        peopleVo.setFamilyId(familyId);
        //设置妻子我的丈夫的信息;
        peopleVo.setHusbandPeopleId(peopleVo.getMyPeopleId());
        //解析出来妻子的参数的信息;
        People peopleWife = ParmIntended.removePeopleRequestId(peopleVo);
        People insertPeopleWife = peopleFunction.insertPeople(peopleWife);
        //设置我妻子的信息;
        myPeople.setWifePeopleId(insertPeopleWife.getId());
        peopleFunction.updateByid(myPeople);
        //查询出来我儿子的信息添加到母亲的下面,并且配置这些孩子的母亲;
        List<PeopleService> peopleFatherService = peopleFunctionService.slectStringLamQuery(JSON.toJSONString(new QueryWrapperVO()
                .eq("people_father_id", myPeople.getId())
        ));
        if (peopleFatherService.size() > 0) {
            treeConfig.addSon(insertPeopleWife, peopleFatherService, 1);
        }
        return new ResponDataVo(myPeople);
    }

    /**
     * 添加丈夫
     *
     * @param peopleVo
     * @return
     */
    public ResponDataVo addHusPeopleId(@RequestBody PeopleVo peopleVo) {
        //判断是否缺少参数;
        String parmAnalysis = ParmIntended.parmAnalysis(peopleVo, Constant.myPeopleId, Constant.NAME, "tenementId", "gender");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断当前的登录者是否有添加的权限;
        People people = peopleFunction.peopleGetByid(peopleVo.getTenementId());
        Long familyId = people.getFamilyId();
        if (familyId == null) {
            return new ResponDataVo("请创建家族后在进行添加");
        }
        if (people.getJurisdiction() == 2) {
            return new ResponDataVo("您没有权限进行添加;");
        }
        //获取到我的信息;
        People myPeople = peopleFunction.peopleGetByid(peopleVo.getMyPeopleId());
        if (myPeople == null) {
            return new ResponDataVo(Constant.errorMessagePeople);
        }
        if (myPeople.getGender().equals(Constant.genderboy)) {
            return new ResponDataVo("男人不可以添加丈夫");
        }
        //我是不是最底层的女儿;
        //查询子女表判断我是不是最低层的女人;
        PeopleService people_below_id = peopleFunctionService.getQueryOne(JSON.toJSONString(new QueryWrapperVO()
                .eq("people_below_id", myPeople.getId())
        ));
        if (people_below_id != null) {
            return new ResponDataVo("您是最底层的女儿,不可以添加丈夫");
        }
        //判断我是否已经添加过了丈夫;
        if (myPeople.getHusbandPeopleId() != null) {
            return new ResponDataVo("您已经有丈夫了,不可以找2个");
        }
        //设置妻子的权限;
        peopleVo.setJurisdiction(2);
        //设置妻子所在的家族;
        peopleVo.setFamilyId(familyId);
        peopleVo.setWifePeopleId(peopleVo.getMyPeopleId());
        //解析出来妻子的参数的信息;
        People peopleWife = ParmIntended.removePeopleRequestId(peopleVo);
        People insertPeopleWife = peopleFunction.insertPeople(peopleWife);
        //判断我是否有父亲和母亲;
        if (myPeople.getFatherPeopleId() == null && myPeople.getMotherPeopleId() == null) {
            Family family = (Family) familyFunction.selectByid(familyId).getData();
            family.setTopPeopleId(insertPeopleWife.getId());
            familyFunction.updateById(family);
        }
        //丈夫的信息上添加上妻子的数据;
        myPeople.setHusbandPeopleId(insertPeopleWife.getId());
        peopleFunction.updateByid(myPeople);
        //查询出来父亲的孩子,添加到父亲下,然后将孩子们的孩子们设置上父亲的信息;
        List<PeopleService> peopleMontherService = peopleFunctionService.slectStringLamQuery(JSON.toJSONString(new QueryWrapperVO()
                .eq("people_monther_id", myPeople.getId())
        ));
        if (peopleMontherService.size() > 0) {
            treeConfig.addSon(insertPeopleWife, peopleMontherService, 0);
        }
        insertPeopleWife.setWifePeopleId(myPeople.getId());
        peopleFunction.updateByid(insertPeopleWife);
        return new ResponDataVo(myPeople);
    }


    /**
     * 添加儿子，添加儿子时,将儿子或者是女儿的信息添加到父亲母亲下,在将儿子添加到我的兄弟姐妹里面,
     * 别且做兄弟姐妹的映射的关系;
     *
     * @param peopleVo
     * @return
     */
    public ResponDataVo addSon(PeopleVo peopleVo) {
        String parmAnalysis = ParmIntended.parmAnalysis(peopleVo, Constant.myPeopleId, Constant.NAME, "tenementId", "gender");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断当前的登录者是否有添加的权限;
        People people = peopleFunction.peopleGetByid(peopleVo.getTenementId());
        if (people == null) {
            return new ResponDataVo("没有当前登录人的信息;");
        }
        Long familyId = people.getFamilyId();
        if (familyId == null) {
            return new ResponDataVo("请创建家族后在进行添加");
        }
        if (people.getJurisdiction() == 2) {
            return new ResponDataVo("您没有权限进行添加;");
        }
        //获取他父亲或者是他母亲的信息;
        People peopleFatherOrMontherl = peopleFunction.peopleGetByid(peopleVo.getMyPeopleId());
        //我是不是最底层的女儿;
        //查询子女表判断我是不是最低层的女人;
        if (peopleFatherOrMontherl.getGender().equals(Constant.genderGirl)) {
            PeopleService people_below_id = peopleFunctionService.getQueryOne(JSON.toJSONString(new QueryWrapperVO()
                    .eq("people_below_id", peopleFatherOrMontherl.getId())));
            if (people_below_id != null) {
                return new ResponDataVo("您是最底层的女儿,不可以添加儿子或者女儿;");
            }
        }
        Integer gender = peopleFatherOrMontherl.getGender();
        Boolean flageFather = false;
        //说明是父亲;
        if (gender.equals(Constant.genderboy)) {
            //说明是父亲;
            peopleVo.setFatherPeopleId(peopleFatherOrMontherl.getId());
            if (peopleFatherOrMontherl.getWifePeopleId() != null) {
                peopleVo.setMotherPeopleId(peopleFatherOrMontherl.getWifePeopleId());
            }
        }
        //说明是母亲;
        if (gender.equals(Constant.genderGirl)) {
            peopleVo.setMotherPeopleId(peopleFatherOrMontherl.getId());
            if (peopleFatherOrMontherl.getHusbandPeopleId() != null) {
                peopleVo.setFatherPeopleId(peopleFatherOrMontherl.getHusbandPeopleId());
            }
        }
        peopleVo.setJurisdiction(2);
        peopleVo.setFamilyId(familyId);
        //解析出来儿子的信息;
        People peopleSonOrGirl = ParmIntended.removePeopleRequestId(peopleVo);

        //设置家族还有权限;
        peopleSonOrGirl.setFamilyId(peopleFatherOrMontherl.getFamilyId());
        peopleSonOrGirl.setJurisdiction(2);
        //添加这个人的信息;
        People insertPeopleSonOrGirl = peopleFunction.insertPeople(peopleSonOrGirl);
        //将儿子或者是女儿和父母下的儿子或者是女儿做一下绑定;
        treeConfig.addBrotherToFastherOrMontherSonGo(peopleFatherOrMontherl, insertPeopleSonOrGirl);
        //将新增加的这个人的信息,添加到父母的下面,设置为父母的儿子或者是女儿;
        treeConfig.addSonOrGirl(peopleFatherOrMontherl, insertPeopleSonOrGirl);
        return new ResponDataVo(insertPeopleSonOrGirl);
    }

    /*
     *
     * 查询族谱;
     * @param family
     * @return
     */
    public ResponDataVo selectFamily(Family family) {
        String parmAnalysis = ParmIntended.parmAnalysis(family, "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断当前的家族是否存在是否存在;
        Family familySelect = (Family) familyFunction.selectByid(family.getId()).getData();
        if (familySelect == null) {
            return new ResponDataVo("没有当前的家族;");
        }
        //获取到顶级的父亲/母亲;
        Long topPeopleId = familySelect.getTopPeopleId();
        People peopleTop = treeConfig.selectPeopleTop(topPeopleId);
        //获取祖先所有的兄弟姐妹的信息;
        List<People> peopleList = treeConfig.brothersSistersPeopleList(peopleTop);
        Map<String, Object> stringObjectMap = new LinkedHashMap<>();
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (peopleTop.getGender().equals(Constant.genderboy)) {
            for (People people : peopleList) {
                Map<String, Object> tree = getTreeBoy(people, null);
                mapList.add(tree);
            }
        } else {
            for (People people : peopleList) {
                Map<String, Object> tree = getWife(people, null);
                mapList.add(tree);
            }
        }
        stringObjectMap.put("treeData", mapList);
        return new ResponDataVo(stringObjectMap);
    }


    /**
     * getTree2
     *
     * @param people
     * @return
     */
    public Map<String, Object> getTreeBoy(People people, Map<String, Object> stringObjectMap) {
        Boolean flag = false;
        if (stringObjectMap != null) {
            flag = true;
        }
        //查询我的基本的信息;
        Map<String, Object> peopleMsg = getPeopleMsg(people);
        //判断我是男还是女;
        /*  if (people.getGender().equals(Constant.genderboy)) {*/
        //查询妻子,判断是否有妻子;
        if (people.getWifePeopleId() != null) {
            //如果有妻子,获取妻子的信息;
            People peopleWife = peopleFunction.peopleGetByid(people.getWifePeopleId());
            Map<String, Object> peopleWifeMsg = getPeopleMsg(peopleWife);
            if (flag == true) {
                stringObjectMap.put("qz", peopleWifeMsg);
            } else {
                peopleMsg.put("qz", peopleWifeMsg);
            }
        }
        List<People> peopleListSon = null;
        //判断我是否有儿子;
        if (people.getGender().equals(Constant.genderboy)) {
            peopleListSon = treeConfig.peopleGetSonOrgirl(people, 1);
        } else {
            peopleListSon = treeConfig.peopleGetSonOrgirl(people, 2);
        }

        List<Map<String, Object>> mapList = new ArrayList<>();
        if (peopleListSon.size() > 0) {
            for (People peopleSon : peopleListSon) {
                Map<String, Object> peopleSonMsg = getPeopleMsg(peopleSon);
                getTreeBoy(peopleSon, peopleSonMsg);
                mapList.add(peopleSonMsg);
            }
            if (flag == true) {
                stringObjectMap.put("child", mapList);
            } else {
                peopleMsg.put("child", mapList);
            }
            //添加儿子的信息;
        } else {
            flag = false;
        }
        return peopleMsg;
    }

    /**
     * 顶级的母亲查询;
     *
     * @param people
     * @param
     * @return
     */
    private Map<String, Object> getWife(People people, Map<String, Object> stringObjectMapWife) {
        Boolean flag = false;
        if (stringObjectMapWife != null) {
            flag = true;
        }
        Map<String, Object> mapMonther = null;
        if (flag == false) {
            mapMonther = new LinkedHashMap<>();
        }
        //查询我的基本的信息;
        Map<String, Object> peopleMsgWife = getPeopleMsg(people);
        if (flag == false) {
            mapMonther.put("qz", peopleMsgWife);
        }
        //查询妻子,判断是否有妻子;
        if (people.getWifePeopleId() != null) {
            //如果有妻子,获取妻子的信息;
            People peopleWife = peopleFunction.peopleGetByid(people.getWifePeopleId());
            Map<String, Object> peopleWifeMsg = getPeopleMsg(peopleWife);
            if (flag == true) {
                stringObjectMapWife.put("qz", peopleWifeMsg);
            } else {
                peopleMsgWife.put("qz", peopleWifeMsg);
            }
        }
        List<People> peopleListSon = null;
        //查询出来我儿子的信息
        if (people.getGender().equals(Constant.genderboy)) {
            peopleListSon = treeConfig.peopleGetSonOrgirl(people, 1);
        } else {
            peopleListSon = treeConfig.peopleGetSonOrgirl(people, 2);
        }

        List<Map<String, Object>> mapList = new ArrayList<>();
        if (peopleListSon.size() > 0) {
            for (People peopleSon : peopleListSon) {
                Map<String, Object> peopleSonMsg = getPeopleMsg(peopleSon);
                getWife(peopleSon, peopleSonMsg);
                mapList.add(peopleSonMsg);
            }
            if (flag == true) {
                stringObjectMapWife.put("child", mapList);
            } else {
                peopleMsgWife.put("child", mapList);
            }
            //添加儿子的信息;
        } else {
            flag = false;
        }
        return peopleMsgWife;
    }


    public Map<String, Object> getPeopleMsg(People people) {
        Map<String, Object> objectMap = new LinkedHashMap();
        objectMap.put("id", people.getId());
        objectMap.put("name", people.getName());
        objectMap.put("img", people.getAvater());
        objectMap.put("gender", people.getGender());
        return objectMap;
    }

    /**
     * 判断是否有父亲母亲是否
     *
     * @param people
     * @return
     */
    public ResponDataVo selectRelation(@RequestBody PeopleVo people) {
        String parmAnalysis = ParmIntended.parmAnalysis(people, "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        Boolean flag = false;
        Map<String, Object> map = new HashMap<>();
        //查询我是否有父亲;
        People peopleMy = peopleFunction.peopleGetByid(people.getId());
        if (people.getCode().equals("1")) {
            if (peopleMy.getHusbandPeopleId() != null) {
                People peopleGetByid = peopleFunction.peopleGetByid(peopleMy.getHusbandPeopleId());
                //判断丈夫是否有父亲;
                if (peopleGetByid.getFatherPeopleId() != null) {
                    map.put("isHaveFather", true);
                } else {
                    map.put("isHaveFather", false);
                }
            } else {
                map.put("isHaveFather", false);
            }
        } else {
            if (peopleMy.getFatherPeopleId() != null) {
                map.put("isHaveFather", true);
            } else {
                //判断我的兄弟姐妹,判断他们是否有父亲;
                List<BrothersSistersPeople> brothersSistersPeople = brothersSistersPeopleFunction.
                        slectStringQuery(JSON.toJSONString(new QueryWrapperVO()
                                .eq("my_people_id", peopleMy.getId())
                        ));
                Boolean flagss = false;
                for (BrothersSistersPeople brothersSistersPerson : brothersSistersPeople) {
                    Long brotherPeopleId = brothersSistersPerson.getBrotherPeopleId();
                    People peopleGetByid = peopleFunction.peopleGetByid(brotherPeopleId);
                    if (peopleGetByid.getFatherPeopleId() != null) {
                        flagss = true;
                        break;
                    }
                }
                map.put("isHaveFather", flagss);
            }
        }
        //说明是妻子;
        if (people.getCode().equals("1")) {
            //判断我有没有丈夫;
            if (peopleMy.getHusbandPeopleId() != null) {
                People peopleGetByid = peopleFunction.peopleGetByid(peopleMy.getHusbandPeopleId());
                //判断丈夫是否有父母;
                if (peopleGetByid.getMotherPeopleId() != null) {
                    map.put("isHaveMonther", true);
                } else {
                    map.put("isHaveMonther", false);
                }
            } else {
                //判断我的兄弟姐妹,判断他们是否有父亲;
                List<BrothersSistersPeople> brothersSistersPeople = brothersSistersPeopleFunction.
                        slectStringQuery(JSON.toJSONString(new QueryWrapperVO()
                                .eq("my_people_id", peopleMy.getId())
                        ));
                Boolean flagss = false;
                for (BrothersSistersPeople brothersSistersPerson : brothersSistersPeople) {
                    Long brotherPeopleId = brothersSistersPerson.getBrotherPeopleId();
                    People peopleGetByid = peopleFunction.peopleGetByid(brotherPeopleId);
                    if (peopleGetByid.getFatherPeopleId() != null) {
                        flagss = true;
                        break;
                    }
                }
                map.put("isHaveMonther", flagss);
            }
        } else {
            if (peopleMy.getMotherPeopleId() != null) {
                if (peopleMy.getMotherPeopleId() != null) {
                    map.put("isHaveMonther", true);
                } else {
                    map.put("isHaveMonther", false);
                }
            }
        }

        Integer gender = peopleMy.getGender();
        if (gender.equals(Constant.genderboy)) {
            //判断有没有妻子;
            if (peopleMy.getWifePeopleId() != null) {
                map.put("isHaveWife", true);
            } else {
                map.put("isHaveWife", false);
            }

        } else {
            flag = true;
            //判断是否有丈夫;
            if (peopleMy.getHusbandPeopleId() != null) {
                map.put("isHaveHusband", true);
            } else {
                map.put("isHaveHusband", false);
            }
        }
        //判断他是不是最底层的女儿;
        if (flag == true) {
            QueryWrapperVO queryWrapperVO = new QueryWrapperVO();
            queryWrapperVO.eq("people_below_id", peopleMy.getId());
            if (peopleMy.getFatherPeopleId() != null) {
                queryWrapperVO.eq("people_father_id", peopleMy.getFatherPeopleId());
            }
            if (peopleMy.getMotherPeopleId() != null) {
                queryWrapperVO.eq("people_monther_id", peopleMy.getMotherPeopleId());
            }
            //查询子女情况;
            PeopleService queryOne = peopleFunctionService.getQueryOne(JSON.toJSONString(queryWrapperVO));
            if (queryOne != null) {
                map.put("isGround", true);
            } else {
                map.put("isGround", false);
            }
        }
        return new ResponDataVo(map);
    }

    /**
     * 删除家族中树上的一个节点;
     * @param
     * @return
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ)
    public ResponDataVo delTreeNode(@RequestBody PeopleVo people) {
        String parmAnalysis = ParmIntended.parmAnalysis(people, "id","tenementId");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        People peopleMyPeople = peopleFunction.peopleGetByid(people.getTenementId());
        if(peopleMyPeople.getJurisdiction()==2){
            return new ResponDataVo("您没有权限进行当前的操作;");
        }
        People peopleDelNode = peopleFunction.peopleGetByid(people.getId());
        if (peopleDelNode == null) {
            return new ResponDataVo("没有发现当前的书的节点;");
        }
        if(peopleDelNode.getJurisdiction()==0){
            return new ResponDataVo("不可以删除当前的家族的创始人;");
        }
        if(peopleDelNode.getJurisdiction()==1){
            return new ResponDataVo("不可以删除管理员;");
        }
        if (peopleDelNode.getWifePeopleId()!=null) {
              return new ResponDataVo("请先删除您的妻子;");
        }
        //true是女人;
        Boolean flag = false;
        //判断当前的这个人是不是在书的最底层;
        QueryWrapperVO queryWrapperVO = new QueryWrapperVO();
        if (peopleDelNode.getGender().equals(Constant.genderGirl)) {
            queryWrapperVO.eq("people_monther_id", people.getId());
        } else {
            queryWrapperVO.eq("people_father_id", people.getId());
        }
        //判断当前的这个人是否有儿子或者是女儿;
        List<PeopleService> peopleServices = peopleFunctionService.slectStringLamQuery(JSON.toJSONString(queryWrapperVO));
        if (peopleServices.size() > 0) {
            return new ResponDataVo("请先删除子女;");
        }
        //判断当前删除的人是不是初始化的节点,如果是的话,是不可以删除的;
        if(peopleDelNode.getMotherPeopleId()==null&&peopleDelNode.getFatherPeopleId()==null){
            if(peopleDelNode.getJurisdiction()==0){
                return new ResponDataVo("不可以删除当前的家族的创始人;");
            }
        }
        if(peopleDelNode.getGender().equals(Constant.genderboy)){
            //男;
            //解除他与妻子之间的关系;
            if(peopleDelNode.getWifePeopleId()!=null){
                People people1 = peopleFunction.peopleGetByid(peopleDelNode.getWifePeopleId());
                people1.setHusbandPeopleId(null);
                /*peopleFunction.updateByid(people1);*/
                peopleMapper.updateHush(people1);
            }
        }else {
            //解除他与丈夫之间的关系;
            //女;
            if(peopleDelNode.getHusbandPeopleId()!=null){
                People people1 = peopleFunction.peopleGetByid(peopleDelNode.getHusbandPeopleId());
                people1.setWifePeopleId(null);
                /*peopleFunction.updateByid(people1);*/
                peopleMapper.updateWife(people1);
            }
        }
        //判断他是否有父亲,解除他与父亲之间的关系;
        if (peopleDelNode.getFatherPeopleId() != null || peopleDelNode.getMotherPeopleId() != null) {
            //解除他与父亲之间的关系;
            PeopleService queryOne = peopleFunctionService.getQueryOne(JSON.toJSONString(new QueryWrapperVO()
                    .eq("people_below_id", peopleDelNode.getId())));
            if(queryOne!=null){
                peopleFunctionService.delectByid(queryOne.getId());
            }
        }
        //
        //将他和他的兄弟和之间的关系进行删除;
        List<BrothersSistersPeople> brothersSistersPeople = brothersSistersPeopleFunction.slectStringQuery(JSON.toJSONString(new QueryWrapperVO()
                .eq("my_people_id", peopleDelNode.getId())
        ));
        if(brothersSistersPeople.size()>0){
            //将我和我兄弟之间的关系进行删除;
            for (BrothersSistersPeople brothersSistersPerson : brothersSistersPeople) {
                //贤删除我绑定的我的兄弟的数据;
                brothersSistersPeopleFunction.delBrother(brothersSistersPerson);
                //在将我兄弟和我之间的关系来进行删除;
                QueryWrapperVO queryWrapperVO1 = new QueryWrapperVO();
                queryWrapperVO1.eq("my_people_id", brothersSistersPerson.getBrotherPeopleId());
                queryWrapperVO1.eq("brother_people_id", peopleDelNode.getId());
                BrothersSistersPeople brothersSistersPeople1 = brothersSistersPeopleFunction.slectQueryGetOne(JSON.toJSONString(queryWrapperVO1));
                brothersSistersPeopleFunction.delBrother(brothersSistersPeople1);
            }
        }
        //将当前的这个人的信息来进行删除;
        peopleMapper.updateDel(peopleDelNode);
        //peopleFunction.delPeopleByid(peopleDelNode.getId());
        People peopleGetByid = peopleFunction.peopleGetByid(peopleDelNode.getId());
        peopleGetByid.setFamilyMergeId(peopleDelNode.getFamilyId());
        peopleFunction.updateByid(peopleGetByid);
        return new ResponDataVo(true);
    }


}










